﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Twi.COMMON.Core
{
    /*
    * Copyright: ©2010-2013 Twilight软件开发工作室版权所有
    * Author: xuzhihong
    * Create date: 2010-4-22
    * Description: TwiIDsHelper
    * 
    */
    public class TwiIDsHelper
    {
        /// <summary>
        /// 获取某节点及所有子节点的ID
        /// </summary>
        /// <param name="dt">整个表数据</param>
        /// <param name="id">传入节点的ID</param>
        /// <returns></returns>
        public static List<string> GetIDs(DataTable dt, string id)
        {
            List<string> listIDs = new List<string>();
            //填充选中的节点ID
            listIDs.Add(id.ToString());
            //填充子节点
            foreach (DataRow dr in dt.Select(string.Format("FParentID='{0}'", id)))
            {
                listIDs.Add(dr["FID"].ToString());
                FillChildern(dt, dr, listIDs);
            }
            return listIDs;
        }

        protected static void FillChildern(DataTable dt, DataRow dr, List<string> listIDs)
        {
            foreach (DataRow row in dt.Select(string.Format("FParentID='{0}'", dr["FID"])))
            {
                listIDs.Add(row["FID"].ToString());
                FillChildern(dt, row, listIDs);
            }
        }


        /// <summary>
        /// 表关系为：（主表）一对多（副表）
        /// </summary>
        /// <param name="dt">副表</param>
        /// <param name="id">主表的一个ID</param>
        /// <param name="field">副表外键字段</param>
        /// <returns></returns>
        public static List<string> GetIDs(DataTable dt, string id, string field)
        {
            List<string> listIDs = new List<string>();
            foreach (DataRow dr in dt.Select(string.Format("{0}='{1}'", field, id)))
            {
                listIDs.Add(dr["FID"].ToString());
            }
            return listIDs;
        }

        /// <summary>
        /// 获取In格式字符串，形如：（'China','USA','England'）
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="colName">列名</param>
        /// <param name="needParentheses">前后是否需要加小括号，默认加</param>
        /// <returns></returns>
        public static string GetInFormatIDs(DataRow[] rows, string colName, bool needParentheses = true)
        {
            if (rows == null || rows.Length == 0)
            {
                return needParentheses ? "('')" : "''";
            }
            string strIDs = "'";
            foreach (DataRow row in rows)
            {
                string s = "" + row[colName];
                strIDs += s + "','";
            }
            strIDs = strIDs.Substring(0, strIDs.Length - 2);
            if (needParentheses)
            {
                strIDs = "(" + strIDs + ")";
            }
            return strIDs;
        }

        /// <summary>
        /// 将List<string>IDs  转化为SQL in 的格式  形如：（'China','USA','England'）
        /// </summary>
        /// <param name="IDs"></param>
        /// <param name="needParentheses">前后是否需要加小括号，默认加</param>
        /// <returns></returns>
        public static string GetInFormatIDs(IList<string> listIDs, bool needParentheses = true)
        {
            if (listIDs == null || listIDs.Count == 0)
            {
                return needParentheses ? "('')" : "''";
            }

            string strIDs = "'";
            foreach (string s in listIDs)
            {
                strIDs += s + "','";
            }
            strIDs = strIDs.Substring(0, strIDs.Length - 2);
            if (needParentheses)
            {
                strIDs ="(" +strIDs +")";
            }            
            return strIDs;
        }

        /// <summary>
        /// 将IDs转化为SQL in 的格式  形如：（'China','USA','England'）
        /// </summary>
        /// <param name="IDs"></param>
        /// <param name="needParentheses">前后是否需要加小括号，默认加</param>
        /// <returns></returns>
        public static string GetInFormatIDs(string[] listIDs,bool needParentheses = true)
        {
            if (listIDs == null || listIDs.Length == 0)
            {
                return needParentheses ? "('')" : "''";
            }

            string strIDs = "'";
            foreach (string s in listIDs)
            {
                strIDs += s + "','";
            }
            strIDs = strIDs.Substring(0, strIDs.Length - 2);
            if (needParentheses)
            {
                strIDs ="(" +strIDs +")";
            } 
            return strIDs;
        }

        /// <summary>
        /// 将strIDs转化为SQL in 的格式  形如：（'China','USA','England'）
        /// </summary>
        /// <param name="strIDs">IDs字符串，中间以separator隔开</param>
        /// <param name="separator">IDs分割符</param>
        /// <returns></returns>
        public static string GetInFormatIDs(string strIDs, char separator = ',', bool needParentheses = true)
        {
            string[] IDs = strIDs.Split(separator);
            return GetInFormatIDs(IDs, needParentheses);
        }
    }
}
